//
//  CenterNewsViewController.m
//  iCarStyle
//
//  Created by liu nian on 14-5-4.
//  Copyright (c) 2014年 liunian. All rights reserved.
//

#import "CenterNewsViewController.h"
#import "ICListView.h"
#import "iCarousel.h"
#import "XTSegmentControl.h"
#import "DetailViewController.h"

#define kIS_IOS7                (kCFCoreFoundationVersionNumber > kCFCoreFoundationVersionNumber_iOS_6_1)
#define kIOS7DIS(X) ([[NSNumber numberWithBool:kIS_IOS7] intValue] * X)

@interface CenterNewsViewController ()<iCarouselDataSource,iCarouselDelegate,ICListViewDelegate>
@property (nonatomic , strong) XTSegmentControl *segmentControl;
@property (nonatomic , strong) iCarousel *carousel;
@property (nonatomic , strong) NSMutableArray   *datasource;
@end

@implementation CenterNewsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor flatWhiteColor]];
    [self initListView];
    [self.view addSubview:self.carousel];

    self.title = @"资讯";
    [self.view addSubview:self.segmentControl];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated{

}

- (void)initListView{

    for (int i = 0; i < 8; i++) {
        ICListView *listView = [[ICListView alloc] initWithFrame:self.carousel.bounds type:XTListViewTypeTableViewCell catalog:i];
        listView.delegate = self;
        [self.datasource addObject:listView];
    }
}

#pragma mark ICListViewDelegate
- (void)listView:(ICListView *)listView didSelectArticle:(id)object atIndex:(NSInteger)index;{
    Article *article = object;
    DetailViewController *detailVC = [[DetailViewController alloc] initWithArticle:article];
    [self.navigationController pushViewControllerWithNavigationControllerTransition:detailVC];
}

#pragma mark
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return 8;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    ICListView *listView = nil;
    listView = [self.datasource objectAtIndex:index];
    if (view == nil)
        {
        view = [[UIView alloc] initWithFrame:carousel.bounds];
        listView.tag = 1;
        [view addSubview:listView];
        }
    else
        {
        ICListView *theListView = (ICListView *)[view viewWithTag:1];
        if (theListView != listView) {
            listView.tag = 1;
            [view insertSubview:listView aboveSubview:theListView];
            [theListView removeFromSuperview];
        }
        }

    [listView viewWillAppear];
    return view;
}

- (void)carouselDidScroll:(iCarousel *)carousel
{
    if (_segmentControl) {

        float offset = carousel.scrollOffset;

        if (offset > 0) {

            [_segmentControl moveIndexWithProgress:offset];
        }
    }
}

- (void)carouselDidEndScrollingAnimation:(iCarousel *)carousel
{
    if (_segmentControl) {

        [_segmentControl endMoveIndex:carousel.currentItemIndex];
    }
}
#pragma mark getter
- (iCarousel *)carousel{
    if (!_carousel) {
        _carousel = [[iCarousel alloc] initWithFrame:CGRectMake(0, 36, 320, CGRectGetHeight(self.view.bounds) - 36 - 64)];
        _carousel.backgroundColor = [UIColor whiteColor];
        _carousel.dataSource = self;
        _carousel.delegate = self;
        _carousel.decelerationRate = 0.7;
        _carousel.type = iCarouselTypeLinear;
        //    _carousel.pagingEnabled = YES;
        //    _carousel.edgeRecognition = YES;
        _carousel.bounceDistance = 0.4;
    }
    return _carousel;
}

- (XTSegmentControl *)segmentControl{
    if (!_segmentControl) {
        NSArray *title = @[@"全部",@"美容知识",@"养护知识",@"维修知识",@"改装知识",@"车居知识",@"汽车用品",@"保险知识"];

        __weak typeof(_carousel) weakCarousel = _carousel;

        _segmentControl = [[XTSegmentControl alloc] initWithFrame:CGRectMake(0, 0, 320, 36) Items:title selectedBlock:^(NSInteger index) {

            [weakCarousel scrollToItemAtIndex:index animated:NO];
        }];
    }
    return _segmentControl;
}

- (NSMutableArray *)datasource{
    if (!_datasource) {
        _datasource = [[NSMutableArray alloc] initWithCapacity:4];
    }
    return _datasource;
}

@end
